package com.zp.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.core.utils.TreeBuildUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.api.entity.SysRoleGroup;
import com.zp.system.entity.SysRoleGroupMenu;
import com.zp.system.entity.bo.SysRoleGroupMenuBo;
import com.zp.system.entity.vo.SysRoleGroupMenuVo;
import com.zp.system.mapper.SysRoleGroupMapper;
import com.zp.system.mapper.SysRoleGroupMenuMapper;
import com.zp.system.service.ISysRoleGroupMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义服务与菜单对照Service业务层处理
 *
 * @author zhang peng
 * @date 2022-12-11
 */
@RequiredArgsConstructor
@Service
public class SysRoleGroupMenuServiceImpl implements ISysRoleGroupMenuService {

    private final SysRoleGroupMenuMapper baseMapper;

    private final SysRoleGroupMapper sysRoleGroupMapper;

    /**
     * 查询自定义服务与菜单对照
     */
    @Override
    public SysRoleGroupMenuVo queryById (String roleGroupId) {
        return baseMapper.selectVoById(roleGroupId);
    }

    /**
     * 查询自定义服务与菜单对照列表
     */
    @Override
    public TableDataInfo<SysRoleGroupMenuVo> queryPageList (SysRoleGroupMenuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysRoleGroupMenu> lqw = buildQueryWrapper(bo);
        Page<SysRoleGroupMenuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询自定义服务与菜单对照列表
     */
    @Override
    public List<SysRoleGroupMenuVo> queryList (SysRoleGroupMenuBo bo) {
        LambdaQueryWrapper<SysRoleGroupMenu> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysRoleGroupMenu> buildQueryWrapper (SysRoleGroupMenuBo bo) {
        LambdaQueryWrapper<SysRoleGroupMenu> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRoleGroupId()), SysRoleGroupMenu::getRoleGroupId, bo.getRoleGroupId());
        lqw.eq(StringUtils.isNotBlank(bo.getMenuId()), SysRoleGroupMenu::getMenuId, bo.getMenuId());
        return lqw;
    }

    /**
     * 新增自定义服务与菜单对照
     */
    @Override
    public Boolean insertByBo (SysRoleGroupMenuBo bo) {
        SysRoleGroupMenu add = BeanUtil.toBean(bo, SysRoleGroupMenu.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRoleGroupId(add.getRoleGroupId());
        }
        return flag;
    }

    /**
     * 修改自定义服务与菜单对照
     */
    @Override
    public Boolean updateByBo (SysRoleGroupMenuBo bo) {
        SysRoleGroupMenu update = BeanUtil.toBean(bo, SysRoleGroupMenu.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (SysRoleGroupMenu entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除自定义服务与菜单对照
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Map<String, Object> roleGroupMenuTree (String roleGroupId) {
        Map<String, Object> map = new HashMap<>();
        SysRoleGroup sysRoleGroup = sysRoleGroupMapper.selectById(roleGroupId);
        if (ObjectUtil.isNotNull(sysRoleGroup)) {
            List<SysRoleGroupMenuVo> list = baseMapper.getRoleGroupMenuList(sysRoleGroup.getRoleGroupId(), sysRoleGroup.getMenuCheckStrictly());
            List<Tree<String>> treeList = buildRoleGroupMenuTreeSelect(list);
            map.put("menuCheckStrictly", sysRoleGroup.getMenuCheckStrictly());
            map.put("treeList", treeList);
            map.put("menuCount", list.size());
        }
        return map;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param voList 自定义服务菜单列表
     * @return 下拉树结构列表
     */
    public List<Tree<String>> buildRoleGroupMenuTreeSelect (List<SysRoleGroupMenuVo> voList) {
        if (CollUtil.isEmpty(voList)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(voList, (menu, tree) ->
                tree.setId(menu.getMenuId())
                        .setParentId(menu.getParentId())
                        .setName(menu.getMenuName())
                        .setWeight(menu.getOrderNum()));
    }
}
